using Tool.PEMath;


namespace Tool.PEPhysx
{
    public class PEBoxCollider : PEColliderBase
    {
        public PEVector3 mSize;
        public PEVector3[] mDir = new PEVector3[3];//轴向

        public PEBoxCollider(ColliderConfig cfg)
        {
            name = cfg.mName;
            mPos = cfg.mPos;
            mSize = cfg.mSize;
            mDir[0] = cfg.mAxis[0];
            mDir[1] = cfg.mAxis[1];
            mDir[2] = cfg.mAxis[2];
        }
        
        /**
         * 检测自己（Box）与BoxCollider的碰撞
         */
        public override bool DetectBoxContact(PEBoxCollider col, ref PEVector3 normal, ref PEVector3 borderAdjust)
        {
            //分离轴算法
            return false;
        }
        
        /**
         * 检测自己（Box）与CylinderCollider的碰撞
         */
        public override bool DetectSphereContact(PECylinderCollider col, ref PEVector3 normal, ref PEVector3 borderAdjust)
        {
            //反转一下就行了
            PEVector3 tmpNormal = PEVector3.zero;
            PEVector3 tmpAdjust = PEVector3.zero;
            bool result = col.DetectBoxContact(this, ref tmpNormal, ref tmpAdjust);
            normal = -tmpNormal;
            borderAdjust = -tmpAdjust;
            return result;
        }

        public override bool DetectPointContact(PEVector3 pointPos)
        {
            //得到box中心到中心的距离向量在x，z轴方向的投影长度
            PEVector3 disOffset = pointPos - mPos;
            disOffset.y = PEInt.zero;
            var dot_x = PEVector3.Dot(disOffset, mDir[0]);
            var dot_z = PEVector3.Dot(disOffset, mDir[2]);
            return dot_x <= mSize[0] && dot_z <= mSize[2];
        }
    }
}

